<?php
use com\gdl\common\dataobject\common\ResultDO; 
use com\gdl\kq\interfaces\AttAdminUserServiceIf;
use com\gdl\kq\dataobject\adminuser\UserDTO; 
use com\gdl\kq\dataobject\adminuser\UserInfoDTO; 
use com\gdl\kq\dataobject\adminuser\UserPositionDTO; 
use com\gdl\kq\dataobject\adminuser\UserEditDTO; 
use com\gdl\kq\dataobject\adminuser\UserEditPhoto; 
use com\gdl\kq\dataobject\adminuser\PositionDTO; 
require(dirname(dirname(__FILE__)) ."/php_sdk-master/include.php");
use MagicCrypt\MagicCrypt;
/**
 * 考勤系统Admin后台用户相关接口 
 * 此接口包含后台用户列表 修改 添加 相关接口
 */
class api_AttAdminUserService extends api_Abstract implements AttAdminUserServiceIf{
	
	# 默认留底照片
	static public $photo = "http://ot7pftuwd.bkt.clouddn.com/jgg8s6pc_264rfqm9vgty5ae187f645c25.jpg";
	
	//获取用户列表
	public function getMemberList(UserDTO $user){
		
		#接受参数
		$data = $user->data ? gdl_lib_BaseUtils::getStr($user->data) : "";
		$offset = $user->offset ? gdl_lib_BaseUtils::getStr($user->offset, 'int') : 0;
        $num = $user->num ? gdl_lib_BaseUtils::getStr($user->num, 'int') : 10;
		$platformId = $data['platformId'] ?$data['platformId']:"";
		$unitId = $data['unitId'] ?$data['unitId']:0;
		$positionId = $data['positionId'] ?$data['positionId']:"";
		$departmentId = $data['departmentId'] ?$data['departmentId']:"";
		$likeName = $data['likeName'] ?$data['likeName']:""; # 姓名 身份证 手机号
		$userId = $data['userId'] ?  $data['userId']:""; #此id用于人员下载
		$type = $data['type'] ?  $data['type']:""; #用于判断该管理员下所有部门的人员 ==all则判断
		$adminUserId = $data['adminUserId']?$data['adminUserId']:"";
		$photoStatus = $data['photoStatus']?$data['photoStatus']:"";
		
		$result = new ResultDO();
        $result->success = true;
        $result->code = 400; 
        $result->data = [];
		$result->counts = 0;
		#验证
		if(empty($data)){$result->message = "缺少必要条件"; return $result;}
		
		#分页
		if($offset=='0') $offset=1;
		$limit = ($offset - 1) * $num . "," . $num;
		$group = 'order by id desc limit '.$limit;
		
		try{
			#用户数据
			$userModel = new model_attendance_member();
			$unitsModel = new model_lhbase_units();
			$authUserModel = new model_lhbase_adminuser(); #超级管理员
			$authUserUnitsModel = new model_lhbase_authuserunits(); #管理员所有的部门id
			$unitList = $unitsModel->operating($platformId, $unitId);
			$where = "status=2 and platformId={$platformId} ";
			$unitList = implode(",", $unitList);
			#查询是否是超级管理员
			$emptyAdmin = $authUserModel->selectOne("isDelete=1 and id={$adminUserId} and unitsId=0 and characters='zg'","id");
			#为空则不存在 不存在就是超级管理员
			if(empty($emptyAdmin)){
				
				if($type=="all"){
					#查询该管理员下所有单位id
					$untisList = $authUserUnitsModel->select("adminUserId={$adminUserId}","unitsId")->items;
					
					$newUntisArray = [];
					foreach($untisList as $key){
						$newUntisArray[] = $key['unitsId'];
					}
					$untisList = implode(",",$newUntisArray);
					#为空不判断，就让他为空
					$where.= " and unitId in ({$untisList}) ";
				}else{
					#查询赛选的值
					$where.= " and unitId in ({$unitList}) ";
				}
			}
			
			#模糊查询用户
			if(!empty($likeName)){
				$where.= " and (realName like '%".$likeName."%' or idcard like '%".$likeName."%' or phone like '%".$likeName."%') ";
			}
			#查询职位
			if(!empty($positionId)){
				$where.= " and positionId={$positionId} ";
			}
			#单差部门
			if(!empty($departmentId)){
				$where.= " and departmentId in ({$unitList}) ";
			}
			if(!empty($photoStatus)){
				$where.= " and photoStatus={$photoStatus} ";
			}
			if(!empty($userId)){
				$wehre  = " status =2 and platformId={$platformId} and id in ({$userId}) ";
			}
			
			
			$userFiled = "id,realName,phone,idcard,departmentId,positionId,photo,photoStatus,unitId,status,isCharge,platformId,jobNumber";
			#查询条件
			
			$userWhere = $where;
			$userList = $userModel->select($userWhere, $userFiled, $group)->items;
			$userCount = $userModel->selectOne($userWhere, 'count(id) as num');
			$result->counts = $userCount['num']?$userCount['num']:0;

			# 处理数据
			if($userList){ 
				#职位数据
				$positionModel = new model_attendance_position();
				$positionList = $positionModel->getAllPosition();
				$positionList = self::setArrKeyId($positionList);
				
				#部门数据
				
				$unitsWhere = "isDelete = 1";
				$unitsFiled = "id,platformId,parentId,unitName";
				$unitsList = $unitsModel->select($unitsWhere, $unitsFiled)->items;
				$unKeyList = self::setArrKeyId($unitsList);
				//var_dump($unKeyList);exit;
				
				#组装数据
				$i = 0;
				foreach($userList as $v){
					$userList[$i]['departmentName'] = $unKeyList[$v['departmentId']]['unitName']?$unKeyList[$v['departmentId']]['unitName']:"暂无部门";
					$userList[$i]['positionName'] = $positionList[$v['positionId']]['title']?$positionList[$v['positionId']]['title']:"暂无职位";
					$userList[$i]['companyName'] = self::getToplevel($v['unitid'],$unitsList);#所在组织
					if(empty($v['phone'])){$userList[$i]['phone']="--";}
					$i++;
				} 

				$result->message = "获取成功";
				$result->code = 200; 
				$result->data = $userList;
			}else{
				$result->code = 200; 
				$result->message = "暂无数据";
			}
			
		}catch(Exception $ex){
			$result->message = $ex->getMessage();
		}
		 
        return $result; 
	}
    // 调整用户职位
	public function editMemberListByPositionId(UserPositionDTO $position){
		$positionId = $position->positionid ? gdl_lib_BaseUtils::getStr($position->positionid) : "";
		$userId = $position->update ? gdl_lib_BaseUtils::getStr($position->update) : "";
		
		$result = new ResultDO();
        $result->success = true;
        $result->code = 400; 
        $result->data = [];
		#验证
		if(empty($positionId)){$result->message = "请选择职位"; return $result;}
		if(empty($userId)){$result->message = "请选择需要修改的用户"; return $result;}
		
		try{
			$userModel = new model_attendance_member();
			$userWhere = "id={$userId}";
			$userData = ['positionId'=>$positionId,'updateTime'=>time()];
			$ret = $userModel->update($userWhere, $userData);
			if($ret){
				$result->message = "调整成功";
				$result->code = 200; 
			}else{
				$result->message = "调整失败"; 
			}
		}catch(Exception $ex){
			$result->message = $ex->getMessage();
		}
		 
        return $result; 
	}
	//添加人员
	public function addUser(UserEditDTO $user){
		#接受参数
		$data = $user->data ? gdl_lib_BaseUtils::getStr($user->data) : "";
		
		#实例化返回
		$result = new ResultDO();
        $result->success = true;
        $result->code = 400; 
        $result->data = [];
		
		#验证
		if(empty($data)){
			$result->message = "缺少必要参数";
			return $result;
		}
		
		#处理密码 加盐
		$data['showpassword'] = $data['password'];
		$data['passsalt'] = parent::random(8);
		$data['password'] = parent::dpassword($data['password'], $data['passsalt']);
		#业务
		try{
			
			$userModel = new model_attendance_member();
			
			#验证 身份证 手机号码不能重复 工号
			if($data['jobNumber']){
				$numberWhere = "jobNumber='".$data['jobNumber']."' and status=2";
				$emptyNumber = $userModel->selectOne($numberWhere,'id');
				if($emptyNumber){ $result->message = "工号已存在，请勿重复添加"; return $result;}
			}
			if(!empty($data['idcard']) && $data['idcard']!="无身份证"){
				$idcardWhere = "idcard='".$data['idcard']."' and status=2 ";
				$emptyIdcard = $userModel->selectOne($idcardWhere,'id');
				if($emptyIdcard){ $result->message = "身份证已存在"; return $result;}
			}			
			if(!empty($data['phone']) && $data['phone']!="无手机号"){
				$phoneWhere = "phone='".$data['phone']."' and status=2 ";
				$emptyPhone = $userModel->selectOne($phoneWhere,'id');
				if($emptyPhone){$result->message = "该手机号已绑定"; return $result;}
			}
			
			#插入数据
			$ret = $userModel->insert($data);
			if($ret){
				$result->code = 200;
				$result->message = "添加成功";
			}else{
				$result->message = "用户添加异常";
			}
		} catch (Exception $e) {
            $result->success = true;
            $result->code = 0;
            $result->message = $e->getMessage();
        }
		 
        return $result; 
	}
	//编辑人员
	public function editMemberInfo(UserEditDTO $user){
		#接受参数
		$data = $user->data ? gdl_lib_BaseUtils::getStr($user->data) : "";
		$userId = $user->userId ? gdl_lib_BaseUtils::getStr($user->userId) : "";
		$agId = $user->agId ? gdl_lib_BaseUtils::getStr($user->agId) : "0";
		
		#实例化返回
		$result = new ResultDO();
        $result->success = true;
        $result->code = 400; 
        $result->data = [];
		
		#验证
		if(empty($data)){
			$result->message = "缺少必要参数";
			return $result;
		}
		if(empty($userId)){$result->message = "請選擇用戶"; return $result;}
		
		#处理密码 加盐
		$data['showpassword'] = $data['password'];
		$data['passsalt'] = parent::random(8);
		$data['password'] = parent::dpassword($data['password'], $data['passsalt']);
		#业务
		try{
			
			$userModel = new model_attendance_member();
			
			#查询用户信息
			$userInfo = $userModel->selectOne('id={$userId}','*');
			
			#验证 身份证 手机号码不能重复 工号
			if($data['jobNumber']){
				$numberWhere = "jobNumber='".$data['jobNumber']."' and status=2 and id<>{$userId}";
				$emptyNumber = $userModel->selectOne($numberWhere,'id');
				if($emptyNumber){ $result->message = "工号已存在，请勿重复添加"; return $result;}
			}
			if(!empty($data['idcard']) && $data['idcard']!="无身份证"){
				$idcardWhere = "idcard='".$data['idcard']."' and status=2 and id<>{$userId}";
				$emptyIdcard = $userModel->selectOne($idcardWhere,'id');
				if($emptyIdcard){ $result->message = "身份证已存在"; return $result;}
			}			
			if(!empty($data['phone']) && $data['phone']!="无手机号"){
				$phoneWhere = "phone='".$data['phone']."' and status=2 and id<>{$userId}";
				$emptyPhone = $userModel->selectOne($phoneWhere,'id');
				if($emptyPhone){$result->message = "该手机号已绑定"; return $result;}
			}
			
			#插入数据
			$upWhere = "id={$userId}";
			$ret = $userModel->update($upWhere, $data);
			if($ret){
				$result->code = 200;
				$result->message = "添加成功";
				#验证是否已经加入考勤组
				$groupUserModel = new model_attendance_attendancegroupuser();
				$groupWhere = "agId={$agId} and status=0 and memberId={$userId} and type=1";
				$groupUser = $groupUserModel->selectOne($groupWhere, 'id');
				if(empty($groupUser)){
					$groupData = ['agId'=>$agId,'memberId'=>$userId,'type'=>1,'platformId'=>$userInfo['platformId'],'createTime'=>time(),'status'=>0];
					$groupRet =$groupUserModel->insert($groupData);
					if(!$groupRet){
						$result->message = "用户编辑成功，但添加考勤组失败";
					}
				}
				
			}else{
				$result->message = "用户添加异常";
			}
		} catch (Exception $e) {
            $result->success = true;
            $result->code = 0;
            $result->message = $e->getMessage();
        }
		 
        return $result;  
	}
	//用户详情
	public function userInfo(UserEditDTO $user){
		#接受参数
		$userId = $user->userId ? gdl_lib_BaseUtils::getStr($user->userId) : "";
			
		$result = new ResultDO();
        $result->success = true;
        $result->code = 400; 
        $result->data = [];
		
		#验证
		if(empty($userId)){$result->message = "请选择用户"; return $result;}
		
		try{
			# 用户详情
			$userModel = new model_attendance_member();
			$userWhere['id'] = $userId;
			$userFiled = "id,userName,realName,idcard,phone,jobnumber,photo,photoStatus,positionId,departmentId,isBind,isCharge,status,platformId,unitId,showPassword";
			$userInfo = $userModel->selectOne($userWhere, $userFiled);
			if($userInfo){
				# 查询考勤组
				$groupUserModel = new model_attendance_attendancegroupuser();
				$groupWhere = "status=0 and memberId={$userId} and type=1";
				$groupUser = $groupUserModel->selectOne($groupWhere, 'id,agId');
				
				#查部门的上级id
				$unitsModel = new model_lhbase_units();
				$unitsWhere = "id = {$userInfo['unitId']}"; 
				$unitsList = $unitsModel->selectOne($unitsWhere, 'id,parentId');
				$userInfo['departmentPid'] = $unitsList['parentId'] ? $unitsList['parentId']:0;
				$userGroupId = $groupUser['agId'] ? $groupUser['agId'] : 0;
				#默认留底
				$userInfo['photo'] = $userInfo['photo']?$userInfo['photo'] : self::$photo;
				$userInfo['agId'] = $userGroupId;
				$result->code = 200; 
				$result->data[] = $userInfo;
				$result->message = "获取成功";
			}else{
				$result->message = "用户不存在";
			}
			
		}catch(Exception $e){
			$result->message = $e->getMessage();
		}
		 
        return $result; 
	}
	//调整人员部门
	public function changeMemberPosition(UserEditDTO $user){
		$userId = $user->userId ? gdl_lib_BaseUtils::getStr($user->userId) : "";
		$departmentId = $user->agId ? gdl_lib_BaseUtils::getStr($user->agId) : "";
		
		$result = new ResultDO();
        $result->success = true;
        $result->code = 400; 
        $result->data = [];
		
		$myUserId = explode(",", $userId);
		$userArray = [];
		foreach($myUserId as $v){
			if(!empty($v)){
				$userArray[] = $v;
			}
		}
		$userId = implode(",", $userArray);
		
		if(empty($departmentId)){$result->message = "请选择需要调整的部门"; return $result;}
		if(empty($userId)){$result->message = "请选择需要调整的人员"; return $result;}
		
		try{
			$userModel = new model_attendance_member();
			$userWhere = "id in({$userId})";
			$upData = ['departmentId'=>$departmentId, 'unitId'=>$departmentId];
			$ret = $userModel->update($userWhere, $upData);
			if($ret){
				$result->message = "调整成功";
				$result->code = 200;
			}else{
				$result->message = "没有做任何调整";
				$result->code = 200;
			}
		}catch(Exception $ex){
			$result->message = $ex->getMessage();
		}
        return $result; 
	}
	
	//审核照片
	public function verifyMemberPhoto(UserEditPhoto $user){
		#接受参数
		$userId = $user->userId ? gdl_lib_BaseUtils::getStr($user->userId) : "";
		$status = $user->status ? gdl_lib_BaseUtils::getStr($user->status) : "";
		$text = $user->text ? gdl_lib_BaseUtils::getStr($user->text) : "";
		$data = $user->data ? gdl_lib_BaseUtils::getStr($user->data) : "";
		$photo = $data['photo'] ? $data['photo'] :"";
		
		$result = new ResultDO();
        $result->success = true;
        $result->code = 400; 
        $result->data = [];
		
		# 验证
		if(empty($userId)){$result->message = "请选择用户"; return $result;}
		
		try{
			$userModel = new model_attendance_member();
			$upData = ['photoInfo'=>$text,'photoStatus'=>$status];
			if(!empty($photo)){$upData['photo'] = $photo;}
			$where['id'] = $userId;
			$ret = $userModel->update($where, $upData);
			if($ret){
				$result->code = 200;
				$result->message = "修改成功";
			}else{
				$result->message = "已修改，请刷新页面";
			}
			
		}catch(Exception $e){
			$result->message = $e->getMessage();
		}
		 
        return $result; 
	}
	
	//导出人员
	public function downMemberExcel(UserDTO $user){
		 
		$result = new ResultDO();
        $result->success = true;
        $result->code = 400; 
        $result->data = [];
		 
        return $result; 
	}
	//删除人员
	public function deleteOneMember(UserInfoDTO $user){
		# 需要删除的用于 逗号分隔字符串 
		$idstr = $user->idstr ? gdl_lib_BaseUtils::getStr($user->idstr) : "";
		
		$result = new ResultDO();
        $result->success = true;
        $result->code = 400; 
        $result->data = [];
		
		# 验证
		if(empty($idstr)){$result->message = "请选择用户"; return $result;}
		
		try{
			$userModel = new model_attendance_member();
			$where = "id in (".$idstr.")";
			$delete['status'] = 1;
			$ret = $userModel->update($where, $delete);
			if($ret){
				$result->message = "删除成功";
				$result->code = 200;
			}else{
				$result->message = "用户已删除，请刷新页面";
			}
				
		}catch(Exception $e){
			$result->message  = $e->getMessage();
		}
		 
        return $result; 
	}
	//批量删除 单个删除已实现批量删除，此方法无需实现
	public function deleteAllMember(UserInfoDTO $user){
		 
		$result = new ResultDO();
        $result->success = true;
        $result->code = 400; 
        $result->data = [];
		 
        return $result; 
	}
	//职位列表
	public function getpositionList(PositionDTO $position){
		 
		$result = new ResultDO();
        $result->success = true;
        $result->code = 400; 
        $result->data = [];
		 
        return $result; 
	}

    //获取平台人员组织架构列表
    public function getUnitsMemberRelationList(UserDTO $user){
        $platformId = $user->platformId ? gdl_lib_BaseUtils::getStr($user->platformId) : "";
        $unitId = $user->unitId ? gdl_lib_BaseUtils::getStr($user->unitId) : 0;
        $data = $user->data ? gdl_lib_BaseUtils::getStr($user->data) : 0;

        $result = new ResultDO();
        $result->success = true;
        $result->code = 400;
        $result->data = [];

        if(empty($platformId)){$result->message = "缺少必要条件"; return $result;}

        $adminUserId = $data['adminUserId']?$data['adminUserId']:0;
        $type = $data['type']?$data['type']:1;
        try{
            $serviceUserModel = new service_user();
            $userFiled = "*";
            $userList = $serviceUserModel->getMemberUnitsRelation($platformId,$adminUserId,$type,'children','',$unitId);
            
            if($userList){
                $result->message = "获取成功";
                $result->code = 1;
                $result->data = ['0'=>[json_encode($userList)]];
            }else{
                $result->code = 0;
                $result->message = "暂无数据";
            }

        }catch(Exception $ex){
            $result->message = $ex->getMessage();
        }

        return $result;
    }
	
	// 人员组织架构统计
	public function getMemberUnitsList(UserDTO $user){
        $platformId = $user->platformId ? gdl_lib_BaseUtils::getStr($user->platformId) : "";
        $unitId = $user->unitId ? gdl_lib_BaseUtils::getStr($user->unitId) : 0;
		$data = $user->data ? gdl_lib_BaseUtils::getStr($user->data) : '';
		$likeName = $data['likeName'] ? $data['likeName']:"";
		$adminUserId = $data['adminUserId']?$data['adminUserId']:0;

        $result = new ResultDO();
        $result->success = true;
        $result->code = 400;
        $result->data = [];
 
		#验证
        if(empty($platformId)){$result->message = "请选择平台"; return $result;}

        try{
            $serviceUserModel = new service_user();
			$globals = new service_globals();
			$userModel = new model_attendance_member();
			$unitsModel = new model_lhbase_units();
			
			#先获取部门
            $resultList = $serviceUserModel->getMemberUnitsRelation($platformId,$adminUserId,2,'_child',$likeName); 
			$unitsList = !empty($resultList['relation']) ? $resultList['relation'] : "";
			
            if($unitsList){
				#计算每个部门下的人数
				foreach ($unitsList as $key=>$value){
					$units[$key]['hasChildren'] = $value['hasChildren'];
					$units[$key]['id'] = $value['id'];
					$units[$key]['level'] = $value['level'];
					$units[$key]['pid'] = $value['pid'];
					$units[$key]['title'] = $value['title'];
					$unitList = $unitsModel->operating($platformId, $value['id']);
					$unitList = implode(",", $unitList);
					#遍历获取用户
					$userWhere = "platformId ={$platformId} and status=2 ";
					if(!empty($unitList)){
						$userWhere.=" and unitId in ({$unitList}) ";
					}
					$userNum = $userModel->select($userWhere, "count(id) as num")->items;
					$units[$key]['userNum'] = $userNum[0]['num'] ? $userNum[0]['num'] : 0;
				}	 
				$list = $globals->list_to_tree($units,'id','pid','children');
				//var_dump($list);exit;
                $result->message = "获取成功";
                $result->code = 200;
                $result->data= ['0'=>[json_encode($list)]];
            }else{ 
                $result->message = "暂无数据";
            }

        }catch(Exception $ex){
            $result->message = $ex->getMessage();
        }

        return $result;
    }
	
	// 部门列表
	public function getDepartmentList(UserDTO $user){
        $platformId = $user->platformId ? gdl_lib_BaseUtils::getStr($user->platformId) : "";
        $unitId = $user->unitId ? gdl_lib_BaseUtils::getStr($user->unitId) : 0;
		$adminUserId = $data['adminUserId']?$data['adminUserId']:0;

        $result = new ResultDO();
        $result->success = true;
        $result->code = 400;
        $result->data = [];
		
		#验证
        if(empty($platformId)){$result->message = "请选择平台"; return $result;}

        try{
            $serviceUserModel = new service_user(); 
            $DepartmentList = $serviceUserModel->getMemberUnitsRelation($platformId,$adminUserId,2); 
			$list = !empty($DepartmentList['relation']) ? $DepartmentList['relation'] : ""; 
            if($list){
				$resultList = [];
				foreach ($list as $key=>$value){ 
					$resultList[$key]['id'] = $value['id'];  
					$resultList[$key]['title'] = $value['title']; 
				}
				
                $result->message = "获取成功";
                $result->code = 200;
                $result->data = ['0'=>[json_encode($resultList)]];
            }else{
                $result->message = "暂无数据";
            }

        }catch(Exception $ex){
            $result->message = $ex->getMessage();
        }

        return $result;
    }
	
	// 单个公司下属部门 单个部门下属职位
	public function getDepartmentChildList(UserDTO $user){
		$platformId = $user->platformId ? gdl_lib_BaseUtils::getStr($user->platformId) : "";
		$unitId = $user->unitId ? gdl_lib_BaseUtils::getStr($user->unitId) : 0;
		
		$result = new ResultDO();
        $result->success = true;
        $result->code = 400;
        $result->data = [];
		
		#验证
		if(empty($platformId)){$result->message = "请选择平台"; return $result;}
		if(empty($unitId)){$result->message = "请选择单位"; return $result;}
		
		try{
			$unitsModel = new model_lhbase_units();
			$where = "(parentId={$unitId} or id = {$unitId}) and platformId={$platformId} and isDelete=1";
			$filed = "id,unitName,parentId as pid";
			
			$unitsList = $unitsModel->select($where, $filed, 'ORDER BY id DESC')->items;
			if($unitsList){
				
				$pList = [];
				$cList = [];
				$unlistLeng = count($unitsList); 
				for($i=0; $i<$unlistLeng; $i++){
					if($unitsList[$i]['id']==$unitId){ 
						$pList = ['id'=>$unitsList[$i]['id'], 'title'=>$unitsList[$i]['unitName'], 'pid'=>$unitsList[$i]['pid'] ];
					}
					if($unitsList[$i]['pid']==$unitId){
						$cList[] = ['id'=>$unitsList[$i]['id'], 'title'=>$unitsList[$i]['unitName'], 'pid'=>$unitsList[$i]['pid'] ];
					} 
				} 
				$newList = ['parent'=>$pList, 'child'=>$cList]; 
				$resultList['list'] = json_encode($newList);
				$result->message = "获取成功";
				$result->code = 200;
				$result->data[] = $resultList;
			}else{
				$result->message = "无分级";
			}
		}catch(Exception $ex){
			$result->message = $ex->getMessage();
		}
		
		return $result;
	}
	//获取考勤组列表
	public function getGroupList(UserDTO $user){
		$platformId = $user->platformId ? gdl_lib_BaseUtils::getStr($user->platformId) : "";
		$unitId = $user->unitId ? gdl_lib_BaseUtils::getStr($user->unitId) : 0;
		$data = $user->data ?$user->data:"";
		$adminUserId = $data['adminUserId'] ?$data['adminUserId']:"";
		
		$result = new ResultDO();
        $result->success = true;
        $result->code = 400;
		$result->data = [];
		
		#验证
		if(empty($platformId)){$result->message = "请选择平台"; return $result;}
		
		try{
			#超级管理员
			$groupModel = new model_attendance_attendancegroup();
			$lbUserModel = new model_lhbase_adminuser();
			$userGroupModel = new model_attendance_adminuserattendancegroup();
			$lbUserWhere = "id={$adminUserId} and unitsId=0";
			$adminUserInfo = $lbUserModel->selectOne($lbUserWhere, "characters");
			if($adminUserInfo['characters']=="zg"){ #超级管理员
				$groupWhere = "platformId={$platformId} and isDelete=0"; #type 1正常考勤组 2公共考勤组 unitId
				$groupUser = $groupModel->select($groupWhere, 'id,title')->items;
			}else{ #普通管理员需要
				$myAdminUserWhere = "adminUserId={$adminUserId}";
				$myAdminUserInfo = $userGroupModel->select($myAdminUserWhere, "attendancegroupId")->items;
				$newGroupId = [];
				$leng = count($myAdminUserInfo);
				for($i=0; $i<$leng; $i++){
					$newGroupId[] = $myAdminUserInfo[$i]['attendancegroupId'];
				}
				$newStr = implode(",",$newGroupId);
				$newWhere = "id in ({$newStr})";
				$groupUser = $groupModel->select($newWhere, "id,title")->items;
			}
			
			if($groupUser){
				$result->code = 200;
				$result->message = "获取成功";
				$result->data = $groupUser;
			}else{
				$result->code = 200;
				$result->message = "暂无考勤组";
			} 
		}catch(Exception $ex){
			$result->message = $ex->getMessage();
		}
        return $result;
	}

	//处理数组
	private function setArrKeyId($arr) {
		if (!isset($arr))
			return false;
		$arrs = [];
		foreach ($arr as $key => $value) {
			$arrs[$value['id']] = $value;
		}
		return $arrs;
	}
	//找到pid为零的祖宗数据 获取部门上级组织名
	private function getToplevel($childId, $list){
		$companyName = "暂无组织";
		if(!is_array($list)){ return $companyName;}
		$pid = 0;
		$leng = count($list);
		for($i=0; $i<$leng; $i++){
			if($list[$i]['id']==$childId){
				$pid = $list[$i]['parentId']; 
			}
		}
		$leng2 = count($list);
		for($j=0; $j<$leng2; $j++){
			if($list[$j]['id']==$pid){
				$companyName = $list[$j]['unitName'];
			}
			if($pid==0){
				if($list[$j]['id']==$childId){
					$companyName = $list[$j]['unitName'];
				}
			}
		}
		return $companyName;
	}
	//根据PID获得所有子级child数据
	Static private function getChild($res,$pid=0){
		$arr = array();
		foreach ($res as $v) {
			if($v['id'] == $pid){
				$arr [] = $v['child'];
				$arr = array_merge($arr,self::getChild($v['child'],$v['pid']));
			}
		}
		return $arr;
	}
}

